package com.iot08.admin.service;

import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Device;
import com.iot08.common.entity.app.DeviceLocation;
import com.iot08.common.response.Response;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.StringUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/12/15 9:24
 * @author: Rock
 */
@Service
public class DeviceLocationService extends BaseService<DeviceLocation> {

    @Resource
    private DeviceService deviceService;
    @Resource
    private ChannelAreaService channelAreaService;

    /**
     * @param imei
     * @param startTime
     * @param endTime
     * @param user
     * @time: 2020/12/15 9:27
     * @author: Rock
     * @version 1.0
     */
    public Response<Object> list(String imei, String startTime, String endTime, SysUser user) {
        Device device = deviceService.findById(imei);
        if (device == null) {
            return Response.failedResponse("设备号不存在！");
        }
        if (user.getType() == Const.CHANNELUSER_TYPE) {
            //获取当前账号区域级别
            if (user.getChannelArea() == null) {
                if (!user.getChannelAff().getChannelCodes().contains(device.getAgent())) {
                    return Response.failedResponse("无权限访问该设备！");
                }
            } else {
                List<String> channelAreaList = channelAreaService.listIdById(user.getChannelAreaId(), true);
                if (device.getChannelAreaId() == null || !channelAreaList.contains(device.getChannelAreaId())) {
                    return Response.failedResponse("无权限访问该设备！");
                }
            }
        }
        Criteria criteria = Criteria.where("i").is(imei);
        if (StringUtil.isNotBlank(startTime) && StringUtil.isNotBlank(endTime)) {
            criteria.and("t").gte(DateUtil.strToTimestamp(startTime, DateUtil.FORMAT_4) / 1000).lte(DateUtil.strToTimestamp(endTime, DateUtil.FORMAT_4) / 1000 + 86400);
        } else if (StringUtil.isNotBlank(startTime)) {
            criteria.and("t").gte(DateUtil.strToTimestamp(startTime, DateUtil.FORMAT_4) / 1000);
        } else if (StringUtil.isNotBlank(endTime)) {
            criteria.and("t").lte(DateUtil.strToTimestamp(endTime, DateUtil.FORMAT_4) / 1000 + 86400);
        }
        Query query = new Query().addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("t")));
        List<DeviceLocation> dlist = mongoTemplate.find(query, DeviceLocation.class);
        LinkedHashMap<String, List<DeviceLocation>> result = new LinkedHashMap<>();
        for (DeviceLocation dl : dlist) {
            if (dl == null && dl.getTime() == null) {
                continue;
            }
            String date = DateUtil.timestampToStr(dl.getTime().longValue() * 1000, DateUtil.FORMAT_4);
            List<DeviceLocation> dls = result.get(date);
            if (dls == null) {
                dls = new ArrayList<>();
                result.put(date, dls);
            }
            dl.setTimeStr(DateUtil.timestampToStr(dl.getTime().longValue() * 1000, DateUtil.FORMAT_1));
            dls.add(0, dl);
        }
        return Response.successResponse(result);
    }

}
